package com.heima.datasync.service.impl;

import com.heima.common.exception.CustException;
import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.service.EsDataService;
import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import org.apache.commons.collections.CollectionUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Service
public class EsDataServiceImpl implements EsDataService {
    @Autowired
    EsService<SearchArticleVO> esService;

    @Autowired
    ApArticleMapper apArticleMapper;

    public static final String ARTICLE_INDEX_NAME = "app_info_article";

    @Override
    public ResponseResult dataInit() {
//        1.先判断索引库是否存在
        boolean isHas = esService.existIndex(ARTICLE_INDEX_NAME);

        // 2.如果不存在 就创建索引库
        if (!isHas){
            esService.createIndex( "{\n" +
                    "    \"mappings\":{\n" +
                    "        \"properties\":{\n" +
                    "            \"id\":{\n" +
                    "                \"type\":\"long\"\n" +
                    "            },\n" +
                    "            \"publishTime\":{\n" +
                    "                \"type\":\"date\"\n" +
                    "            },\n" +
                    "            \"layout\":{\n" +
                    "                \"type\":\"integer\"\n" +
                    "            },\n" +
                    "            \"images\":{\n" +
                    "                \"type\":\"keyword\",\n" +
                    "                \"index\": false\n" +
                    "            },\n" +
                    "           \"staticUrl\":{\n" +
                    "                \"type\":\"keyword\",\n" +
                    "                \"index\": false\n" +
                    "            },\n" +
                    "            \"authorId\": {\n" +
                    "          \t\t\"type\": \"long\"\n" +
                    "       \t\t},\n" +
                    "          \"title\":{\n" +
                    "            \"type\":\"text\",\n" +
                    "            \"analyzer\":\"ik_smart\"\n" +
                    "          }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",ARTICLE_INDEX_NAME);
        }
        //3.去article 数据库中 查询满足条件的文章数据
        List<ApArticle> allArticles = apArticleMapper.findAllArticles();
        if (CollectionUtils.isEmpty(allArticles)){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"未查询到文章数据");
        }
        //4.将所有的ApArticle 转换为 SearchArticleVo
        List<SearchArticleVO> searchArticleVOList = allArticles.stream().map(apArticle -> {
            SearchArticleVO articleVO = new SearchArticleVO();
            BeanUtils.copyProperties(apArticle, articleVO);
            return articleVO;
        }).collect(Collectors.toList());
        //5.将文章集合 批量的导入到es索引库中
        esService.saveBatch(searchArticleVOList,ARTICLE_INDEX_NAME);
        return ResponseResult.okResult();
    }
}
